package office.service.sale.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import current.security.user.domain.User;

import office.action.sale.form.CustomerConditionForm;
import office.action.sale.form.CustomerForm;
import office.action.sale.form.CustomerInfoForm;
import office.action.sale.wrapper.CustomerWrapper;
import office.action.workflow.form.RemindForm;
import office.bo.sale.impl.Customer;
import office.bo.sale.impl.CustomerFamily;
import office.bo.sale.impl.CustomerInfo;
import office.bo.sale.impl.CustomerProp;
import office.domainset.BizDomainProviderSetImpl;
import office.service.sale.ICustomerFamilyService;
import office.service.sale.ICustomerService;
import office.service.workflow.IRemindService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class CustomerServiceImpl extends BaseBizServiceImpl implements ICustomerService{
	private  IRemindService remindService;
	private ICustomerFamilyService customerFamilyService;
	public void editCustomer(CustomerForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Customer bo = (Customer)domainProviderSetImpl.getCustomerDP().newEmptyObj();
			bo =(Customer) form.copyPropertiesToBO(bo);
			for (Iterator<CustomerInfoForm> iterator = form.getSetInfo().iterator(); iterator.hasNext();) {
				CustomerInfoForm customerInfoForm = (CustomerInfoForm) iterator.next();
				CustomerInfo customerInfo = new CustomerInfo();
				CustomerProp customerProp = new CustomerProp();
				customerProp.setId(customerInfoForm.getCustomerPropId());
				if(customerInfoForm != null && customerInfoForm.getId() != null && customerInfoForm.getId().equals("")){
					customerInfoForm.setId(null);
				}
				customerInfo.setId(customerInfoForm.getId());
				customerInfo.setContent(customerInfoForm.getContent());
				customerInfo.setCustomerProp(customerProp);
				customerInfo.setCustomer(bo);
				bo.getSetInfo().add(customerInfo);
			}
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑客户失败!", e);
		}
	}
	
	public Page getPageInfo(CustomerConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "Customer.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getName())){
				condMap.put("name", condition.getName());
			}
			if(null!=condition&&!Util.isNull(condition.getWriteMan())){
				condMap.put("writeMan", condition.getWriteMan());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findCustomerByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<CustomerWrapper> findAllCustomer(CustomerConditionForm condition,Page page)throws BusinessException{
		List<CustomerWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getName())){
				condMap.put("name", condition.getName());
			}
			if(null!=condition&&!Util.isNull(condition.getWriteMan())){
				condMap.put("writeMan", condition.getWriteMan());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findCustomerByIdAndName",condMap,new HashMap(),page);
			CustomerWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Customer bo = (Customer)list.get(i);
				User user=bo.getUser(domainProviderSetImpl);
				if(null!=user){
					bo.setSalesManName(user.getCname());
				}
				wrapper = new CustomerWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取客户列表失败！", e);
		}
		return wrapperList;
	}

	public void delCustomerById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerDP();
			List<CustomerFamily> c = customerFamilyService.getCustomerFamilyBuCustomerId(id);
			for (Iterator<CustomerFamily> iterator = c.iterator(); iterator.hasNext();) {
				CustomerFamily customerFamily = (CustomerFamily) iterator.next();
				customerFamilyService.delCustomerFamilyById(customerFamily.getId().toString());
			}
			Map map = new HashMap();
			map.put("customerId", id);
			domainProviderSetImpl.getDao().exeByHqlNameAndDomian(provider, "deleteCustomerInfoByCustomerId", map);
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除客户失败！", e);
		}
	}
	
	public CustomerForm getById(String id)throws BusinessException{
		CustomerForm form = new CustomerForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Customer bo = (Customer)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCustomerDP(), id);
			User user=bo.getUser(domainProviderSetImpl);
			if(null!=user){
				bo.setSalesManName(user.getCname());
			}
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取客户失败!", e);
		}
		return form;
	}
	public List<CustomerWrapper> findAllCustomer(CustomerConditionForm condition)throws BusinessException{
		List<CustomerWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getWriteMan())){
				condMap.put("writeMan", condition.getWriteMan());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findAllCustomer",condMap,new HashMap());
			CustomerWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Customer bo = (Customer)list.get(i);
				wrapper = new CustomerWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取客户列表失败！", e);
		}
		return wrapperList;
		
	}
	
	public void sendNote() throws BusinessException {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			StringBuffer  buffer=new StringBuffer();
			buffer.append("select  t.* from  SALE_Customer t ,");
			buffer.append(" ( select   t.salesman  as salesman, max(t.updatedate) as updatedate from  SALE_Customer t  ");
			buffer.append("  where (sysdate-t.updatedate  )>25  ");
			buffer.append("  group by  t.salesman ) tt ");
			buffer.append("  where t.updatedate=tt.updatedate and  t.salesman=tt.salesman ");
			List list=domainProviderSetImpl.getDao().getBySql(buffer.toString(), Customer.class);
			if(!list.isEmpty()){
				for (Object item : list) {
					Customer bo=(Customer)item;
					RemindForm form=buildRemindFormByCustomer(bo);
					remindService.editRemind(form);
				}
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("发送客户提醒失败!", e);
		}
	}
	
	private  RemindForm buildRemindFormByCustomer(Customer  customer) throws BusinessException{
		String msg="您这个月没有对客户进行过任何操作";
		RemindForm form=new RemindForm();
		form.setObjId(customer.getId().toString());
		form.setObjClassName("Customer");
		form.setName(msg);
		form.setSendDate(new Date());
		form.setRemindUser(customer.getSalesMan());
		form.setHandleState(0);
		return form;
	}

	public IRemindService getRemindService() {
		return remindService;
	}

	public void setRemindService(IRemindService remindService) {
		this.remindService = remindService;
	}

	public ICustomerFamilyService getCustomerFamilyService() {
		return customerFamilyService;
	}

	public void setCustomerFamilyService(
			ICustomerFamilyService customerFamilyService) {
		this.customerFamilyService = customerFamilyService;
	}
	
}
